package com.binhai.dept;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【t_org】的数据库操作Service实现
* @createDate 2025-04-24 09:35:59
*/
@Service
public class deptServiceImpl extends ServiceImpl<deptMapper, dept> {
    @Autowired
    deptMapper deptMapper;

    public List<dept> getAllInfos() {
        System.out.println("1122");
        List<dept> depts = deptMapper.selectList(null);
        System.out.println(depts);
        return depts;

    }

    public Map addDept( dept dept) {
        Map map = new HashMap();
        try {
            // 插入数据
            deptMapper.insert(dept);
            map.put("result","success");
            return map;
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
            return map;
        }
    }


    public Map deleteInfo(int id) {
        Map map = new HashMap();
        try {
            deptMapper.deleteById(id);
            map.put("result","success");
            return map;
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
            return map;
        }
    }


    public Map updateInfo(dept d) {
        Map map = new HashMap();
        try {
            deptMapper.updateById(d);
            map.put("result","success");
            return map;
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
            return map;
        }
    }

    // 实现条件查询方法
    public List<dept> selectByWhere(String name, String manager) {
        QueryWrapper<dept> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        if (manager != null && !manager.isEmpty()) {
            queryWrapper.like("manager", manager);
        }
        return deptMapper.selectList(queryWrapper);
    }

    public dept getInfoById(int id) {
        return  deptMapper.selectById(id);
    }
}